package de.hundezimmer.ui;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

import de.hundezimmer.db.MySQL;

@SuppressWarnings("serial")
@Theme("hundezimmer")
public class HundezimmerUI extends UI {

	@WebServlet(value = "/*", asyncSupported = true)
	@VaadinServletConfiguration(productionMode = false, ui = HundezimmerUI.class, widgetset = "de.hundezimmer.ui.widgetset.HundezimmerWidgetset")
	public static class Servlet extends VaadinServlet {
	}
	private String basepath;

	private Connection conn;

	public HundezimmerUI() {
		basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
		try {
			MySQL dbConnection = new MySQL(basepath);
			conn = dbConnection.getConn();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void init(VaadinRequest request) {
		final VerticalLayout layout = new VerticalLayout();
		layout.setMargin(true);
		setContent(layout);

		// Image logo = new Image("", new FileResource(new File(basepath + "/WEB-INF/images/image.png")));
		// logo.setAlternateText("Logo nicht gefunden");
		// layout.addComponent(logo);

		TabSheet tabsheet = new TabSheet();
		layout.addComponent(tabsheet);

		tabsheet.addTab(new showCustomers(conn), "Kunden");
		tabsheet.addTab(new showDogs(conn), "Hunde");
		tabsheet.addTab(new showInjections(conn), "Impfungen");
		tabsheet.addTab(new showInvoice(conn), "Rechnungen");
	}

}